/*    

    bobbietalk - a visual programming environment for toy robotics

    Copyright (C) 2013  Roberto Tiella

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package bobbietalk.semantics.compiler;

import bobbietalk.gui.ProgrammingBlock;
import bobbietalk.gui.blocks.BeginBlock;
import bobbietalk.gui.blocks.EndBlock;
import bobbietalk.gui.blocks.MiddleBlock;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 *
 * @author roberto
 */
public class BlockGraph {

    final ResourceBundle bundle = java.util.ResourceBundle.getBundle("bobbietalk/Labels");
    private List<ProgrammingBlock> blocks;

    static public BlockGraph fromBlocks(List<ProgrammingBlock> blocks) {

        BlockGraph g = new BlockGraph();
        g.blocks = blocks;

        return g;
    }

    public boolean checkSemantics(List<String> problems) {

        boolean res = true;
        ArrayList<String> messages = new ArrayList<>();

        if (!onlyOneEntry()) {
            problems.add(bundle.getString("semantics.checker.onlyoneentry"));
            res = false;
        }

        if (!onlyOneExit()) {
            problems.add(bundle.getString("semantics.checker.onlyoneexit"));
            res = false;
        }

        if (!isConnected(messages)) {
            problems.add(bundle.getString("semantics.checker.connectedprogram"));
            problems.addAll(messages);
            res = false;

        }

        return res;

    }

    private boolean onlyOneEntry() {

        int c = 0;

        for (ProgrammingBlock b : blocks) {

            if (b instanceof BeginBlock) {
                c++;
            }

        }

        return c == 1;

    }

    private boolean onlyOneExit() {

        int c = 0;

        for (ProgrammingBlock b : blocks) {

            if (b instanceof EndBlock) {
                c++;
            }

        }

        return c == 1;

    }

    private boolean isConnected(List<String> message) {

        for (ProgrammingBlock b : blocks) {

            if (b instanceof BeginBlock) {

                if (b.getNextBlock() == null) {

                    message.add(bundle.getString("semantics.checker.beginconnected"));
                    return false;
                }

            } else if (b instanceof EndBlock) {

                if (b.getPreviousBlock() == null) {

                    message.add("end block is not connected");
                    return false;
                }

            } else if (b instanceof MiddleBlock) {

                if (b.getPreviousBlock() == null || b.getNextBlock() == null) {

                    message.add("a block of type " + b.getClass().getSimpleName() + " is not connected");
                    return false;
                }

            }

        }

        return true;
    }

    public BeginBlock getBeginBlock() {

        for (ProgrammingBlock b : blocks) {

            if (b instanceof BeginBlock) {
                return (BeginBlock) b;
            }

        }

        // should never reach this point
        throw new IllegalStateException("should never reach this point");

    }
}
